﻿/****************************** Module Header ******************************\

Copyright (c) Gary Davidson

This source is subject to the Microsoft Public License.
See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
All other rights reserved.

THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;

using System.Text;
using System.Xml.Linq;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;

namespace Savviety.ServiceManager.Facade.Criteria
{
    public partial class CriteriaBuilder
    {
        protected XNamespace NameSpace = XNamespace.Get("http://Microsoft.EnterpriseManagement.Core.Criteria/");
        protected readonly List<string> managagementpacks = new List<string>();
        protected readonly List<string> aliases = new List<string>();
        protected XElement Root;

        public CriteriaBuilder(string nameSpace)
        {
            NameSpace = XNamespace.Get(nameSpace);
            Root = new XElement(NameSpace + "Criteria");
        }
        public CriteriaBuilder()
        {
            Root = new XElement(NameSpace + "Criteria");
        }
        public void AddReferences(string managementPackName, string alias)
        {
            ////Guard.NotNullOrEmpty(() => managementPackName, managementPackName);
            ////Guard.NotNullOrEmpty(() => alias, alias);


            if (string.IsNullOrEmpty(managementPackName))
                throw new ArgumentNullException(managementPackName);
            if (string.IsNullOrEmpty(alias))
                throw new ArgumentNullException(alias);

            managagementpacks.Add(managementPackName);
            aliases.Add(alias);
            var referenceElement = GetReferenceElement(managementPackName, alias);
            Root.Add(referenceElement);

        }
        public void AddNotEqualsCriteria(string property, string value)
        {
            ////Guard.NotNullOrEmpty(() => property, property);
            ////Guard.NotNullOrEmpty(() => value, value);

            var simpleExpression = new SimpleExpression(NameSpace, property, value, ExpressionOperator.NotEquals);
            Root.Add(simpleExpression.ToXElement());

        }

        public void AddCriteria(string property, string value, string @operator)
        {
           // //Guard.NotNullOrEmpty(() => property, property);
           // //Guard.NotNullOrEmpty(() => value, value);


            var simpleExpression = new SimpleExpression(NameSpace, property, value, @operator);
            Root.Add(simpleExpression.ToXElement());

        }

        public void AddEqualsCriteria(string property, string value)
        {
           // //Guard.NotNullOrEmpty(() => property, property);
          //  //Guard.NotNullOrEmpty(() => value, value);

            var simpleExpression = new SimpleExpression(NameSpace, property, value, ExpressionOperator.Equal);
            Root.Add(simpleExpression.ToXElement());

        }
    }

    public  class ProjectionCriteriaBuilder : CriteriaBuilder
    {

        //TODO What about different versions?
        protected ProjectionCriteriaBuilder(string nameSpace) : base(nameSpace)
        {
        }

        public ProjectionCriteriaBuilder() :  base()
        {

        }

        

        public ObjectProjectionCriteria CreateCriteria(ManagementPackTypeProjection typeProjection, CriteriaElement element)
        {
            //var root = GetRoot();
            Root.Add(element.ToXElement());
            return new ObjectProjectionCriteria(Root.ToString(), typeProjection, SCSMServer.CurrentManagementGroup);
        }

        public ObjectProjectionCriteria CreateSimpleCriteria(ManagementPackTypeProjection typeProjection, string property, string value)
        {
            var simpleExpression = new SimpleExpression(NameSpace, property, value, ExpressionOperator.Equal);
            Root.Add(simpleExpression.ToXElement());
            return new ObjectProjectionCriteria(Root.ToString(), typeProjection, SCSMServer.CurrentManagementGroup);
        }

    

    }

    public class ObjectCriteriaBuilder : CriteriaBuilder
    {
        private ManagementPackClass mClass;

        public ObjectCriteriaBuilder()
            : base()
        {

        }

        public ObjectCriteriaBuilder(ManagementPackClass managementPackClass)
            : base()
        {
            //Guard.NotNull(() => managementPackClass, managementPackClass);
            mClass = managementPackClass;
        }
        
      

      

        public EnterpriseManagementObjectCriteria GetCriteria()
        {
            return new EnterpriseManagementObjectCriteria(Root.ToString(),mClass, SCSMServer.CurrentManagementGroup);
        }

       


    }
}
